<template>
    <div>
        <el-table :data="user_list" border style="width: 100%">
            <el-table-column prop="uid" label="用户编号" width="180">
            </el-table-column>
            <el-table-column prop="username" label="用户名称" width="180">
            </el-table-column>
            <el-table-column prop="rolename" label="所属角色">
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button type="primary" size="small" @click="edit(item.row.uid)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.uid)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- 
           page-size	每页显示条目个数，支持 .sync 修饰符	number  默认10条
           current-change	currentPage 改变时会触发	当前页
         -->
        <el-pagination :page-size="size" @current-change="getCurrentPage" background layout="prev, pager, next"
            :total="countNum">
        </el-pagination>
    </div>
</template>
<script>
import { mapActions, mapGetters, mapMutations } from 'vuex';
import { userDelete } from '../../request/api';
export default {
    data() {
        return {}
    },
    computed: {
        ...mapGetters({
            countNum: 'user/getCount',
            size: 'user/getPageSize',
            user_list: 'user/getUserList'
        })
    },
    methods: {
        ...mapActions({
            userList: 'user/getUserListAction',//管理员列表
            count: 'user/getCountAction',//总条数
            changeNum: 'user/changePageNum'
        }),
        ...mapMutations({
            change_num: 'user/CHANGE_PAGENUM'
        }),
        // 删除
        del(uid) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                userDelete({ uid }).then(res => {
                    if (res.code == 200) {
                        // 删除成功
                        this.$message.success(res.msg);
                        // 重新渲染视图
                        this.userList();
                        // 重新获取总条数
                        this.count();
                    } else {
                        // 删除失败
                        this.$message.error(res.msg);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        // 编辑
        edit(uid) {
            // console.log('编辑', uid);
            // 通知父组件  打开弹窗
            this.$emit('edit', uid)
        },
        // 获取当前页码
        getCurrentPage(num) {
            // 将分页的总页码  总条数  每页需要展示几条数据存储到vuex
            // 将当前页码传递给vuex 进行切换
            this.changeNum(num);
        }
    },
    mounted() {
        this.userList()
        this.count();
        this.changeNum(1)
    },
    watch: {},
    filters: {},
    components: {},
}
</script>
<style scoped>
.el-pagination {
    float: right;
}
</style>